#include "admin.h"
#include "ui_admin.h"

#include <QScrollBar>
#include<QMessageBox>

admin::admin(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::admin)
{
    ui->setupUi(this);
    desk=4;
    ui->frame_12->setVisible(false);
    ui->frame_13->setVisible(false);
    ui->frame_14->setVisible(false);
    ui->frame_15->setVisible(false);
    frame=nullptr;
}

admin::~admin()
{
    delete ui;
    delete[] label;
    delete frame;
}

//坐下的槽函数
void admin::slot_sitdown(std::string desknum)
{
    if(std::stoi(desknum)>this->desk)
    {
        QMessageBox msgBOX;
        msgBOX.setWindowTitle("Attention");
        msgBOX.setText("Sorry, this table is not open");
        msgBOX.setStandardButtons(QMessageBox::Ok);
        msgBOX.exec();
    }
    else
    {
        //设置字体族
        QFont font("微软雅黑",10);

        //临时的对象，来保存要改的对象名称
        QScrollArea* scrollarea=nullptr;

        if("1"==desknum)    {content[0]+="Table 1 take a seat.\n"; scrollarea=ui->scrollArea;}
        if("2"==desknum)    {content[1]+="Table 2 take a seat.\n"; scrollarea=ui->scrollArea_2;}
        if("3"==desknum)    {content[2]+="Table 3 take a seat.\n"; scrollarea=ui->scrollArea_3;}
        if("4"==desknum)    {content[3]+="Table 4 take a seat.\n"; scrollarea=ui->scrollArea_4;}
        if("5"==desknum)    {content[4]+="Table 5 take a seat.\n"; scrollarea=ui->scrollArea_5;}
        if("6"==desknum)    {content[5]+="Table 6 take a seat.\n"; scrollarea=ui->scrollArea_6;}
        if("7"==desknum)    {content[6]+="Table 7 take a seat.\n"; scrollarea=ui->scrollArea_7;}
        if("8"==desknum)    {content[7]+="Table 8 take a seat.\n"; scrollarea=ui->scrollArea_9;}

        int s=std::stoi(desknum);
        label[s-1].setFont(font);

        label[s-1].setText(QString::fromStdString(content[s-1]));

        // 设置标签的尺寸策略，确保标签的大小适应内容
        label[s-1].setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);

        // 将标签添加到滚动区域中
        scrollarea->setWidget(&label[s-1]);

        // 将滚动条移动到顶部位置
        scrollarea->verticalScrollBar()->setValue(scrollarea->verticalScrollBar()->minimum());
    }
}

//提交订单的槽函数--此处content1传入的是menuview中添加的菜单内容
void admin::slot_SubmitOrder(std::string content1,std::string desknum)
{
    int s=std::stoi(desknum);
    content[s-1]+=content1;
    label[s-1].setText(QString::fromStdString(content[s-1]));

}

//支付的槽函数
void admin::slot_Pay(uint64_t total,std::string desknum)
{
    int s=std::stoi(desknum);
    content[s-1]+="Consumer has completed the payment!\nTotal:￥"+std::to_string(total)+"\n";
    label[s-1].setText(QString::fromStdString(content[s-1]));
}

//呼叫服务员
void admin::slot_Callwaiter(std::string desknum)
{
    int s=std::stoi(desknum);
    content[s-1]+="Desk "+desknum+" call for a waiter!\n";
    label[s-1].setText(QString::fromStdString(content[s-1]));

}

void admin::on_pushButton_clicked()
{
    content[0]="";
    this->label[0].setText("");
    emit State(1);

}


void admin::on_pushButton_2_clicked()
{
    content[1]="";
    this->label[1].setText("");
    emit State(2);
}


void admin::on_pushButton_3_clicked()
{
    content[2]="";
    this->label[2].setText("");
    emit State(3);
}


void admin::on_pushButton_4_clicked()
{
    content[3]="";
    this->label[3].setText("");
    emit State(4);
}


void admin::on_pushButton_5_clicked()
{
    content[4]="";
    this->label[4].setText("");
    emit State(5);
}


void admin::on_pushButton_6_clicked()
{
    content[5]="";
    this->label[5].setText("");
    emit State(6);
}


void admin::on_pushButton_7_clicked()
{
    content[6]="";
    this->label[6].setText("");
    emit State(7);
}


void admin::on_pushButton_8_clicked()
{
    content[7]="";
    this->label[7].setText("");
    emit State(8);
}

//remove
void admin::on_pushButton_10_clicked()
{
    if(this->desk==4)
    {
        QMessageBox msgBOX;
        msgBOX.setText("The minimum number of tables is 4");
        msgBOX.setStandardButtons(QMessageBox::Ok);
        msgBOX.exec();
    }
    else
    {
        // 构造要查找的帧的名称
        QString frameObjectName = "frame_" + QString::number(desk + 7);

        // 使用 QObject::findChild 查找对应的帧对象
        frame = findChild<QFrame *>(frameObjectName);
        frame->setVisible(false);
        emit ModifyDesk(-1);

        //去除后，附上点击了clean按钮
        QString pushButtonName="pushButton_" + QString::number(desk);
        QObject *foundObject = frame->findChild<QObject *>(pushButtonName);
        QPushButton *buttonToClick = static_cast<QPushButton *>(foundObject);
        buttonToClick->click();  // 执行按钮点击事件

        this->desk--;
    }
}


void admin::on_pushButton_9_clicked()
{
    if(this->desk==8)
    {
        QMessageBox msgBOX;
        msgBOX.setText("The maximum number of tables is 8");
        msgBOX.setStandardButtons(QMessageBox::Ok);
        msgBOX.exec();
    }
    else
    {
        this->desk++;
        // 构造要查找的帧的名称
        QString frameObjectName = "frame_" + QString::number(desk + 7);

        // 使用 QObject::findChild 查找对应的帧对象
        frame = findChild<QFrame *>(frameObjectName);
        frame->setVisible(true);
        emit ModifyDesk(1);
    }
}

